package com.aquarium.mail;

import java.util.Date;
import java.util.Properties;
import javax.mail.Address;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.net.ssl.SSLSocketFactory;

import android.content.Context;
import android.util.Log;

public class MailSender {
	String TAG = "MailSender";
	final  String SSL_FACTORY  =   "javax.net.ssl.SSLSocketFactory" ;
	Context context;
	MailInfo mailInfo;

	public MailSender(Context context) {
		this.context = context;
		mailInfo = MailInfo.getInstance(context);
	}

	public Properties getProperties() {
		Properties properties = new Properties();
		if (mailInfo.getMailServerHost() == null) {
			Log.e(TAG, "mailInfo.getMailServerHost() == null");
			return null;
		}

		properties.put("mail.smtp.host", mailInfo.getMailServerHost());
		if (mailInfo.getIsSSL().equals("true")) {
			properties.put("mail.smtp.user", mailInfo.getUserName());
			properties.put("mail.smtp.socketFactory.port",
					mailInfo.getMailServerPort());
			properties.put("mail.smtp.socketFactory.class", SSL_FACTORY);
			properties.put("mail.smtp.socketFactory.fallback", "false");
			// 当SMTP需要SSL验证时，需要设定
			properties.put("mail.smtp.starttls.enable", "true");
			// 设置ssl需要设置端口，若不设置请不要设置端口，否则报 isSSL err
			properties.put("mail.smtp.port", mailInfo.getMailServerPort());
		}

		properties.put("mail.smtp.auth", mailInfo.validate ? "true" : "false");
		properties.put("mail.debug", "true");
		return properties;
	}

	public boolean sendTextMail(String content) {

		// 判断是否需要身份认证
		MyAuthenticator authenticator = null;
		if (mailInfo.validate) {
			// 如果需要身份认证，则创建一个密码验证器
			authenticator = new MyAuthenticator(mailInfo.getUserName(),
					mailInfo.getPassword());
		}
		Properties properties = getProperties();
		if (properties == null) {
			Log.e(TAG, "properties==null");
			return false;
		}
		// 根据邮件会话属性和密码验证器构造一个发送邮件的session
		Session sendMailSession = Session.getInstance(getProperties(),
				authenticator);
		Log.d("", authenticator.getPasswordAuthentication()
				+ authenticator.password + authenticator.userName);
		try {
			// 根据session创建一个邮件消息
			Message mailMessage = new MimeMessage(sendMailSession);
			// 创建邮件发送者地址
			String fromAddress = mailInfo.getUserName();
			Address from = new InternetAddress(fromAddress);
			// 设置邮件消息的发送者
			mailMessage.setFrom(from);
			// 创建邮件的接收者地址，并设置到邮件消息中
			Address tos = new InternetAddress(mailInfo.getToAddress());
			// Message.RecipientType.TO属性表示接收者的类型为TO
			mailMessage.setRecipient(Message.RecipientType.TO, tos);
			// 设置邮件消息的主题
			mailMessage.setSubject(mailInfo.getSubject());
			// 设置邮件消息发送的时间
			mailMessage.setSentDate(new Date());
			// 设置邮件消息的主要内容
			mailMessage.setText(content);
			// 发送邮件

			Transport.send(mailMessage);

			// Transport transport = sendMailSession.getTransport("smtps");
			// transport.connect (mailInfo.getMailServerHost(), 465,
			// mailInfo.getUserName(), mailInfo.getPassword());
			// transport.send(mailMessage);
			// transport.sendMessage(mailMessage,
			// mailMessage.getAllRecipients());
			// transport.close();

			return true;
		} catch (MessagingException ex) {
			ex.printStackTrace();
		}
		return false;
	}

}
